import axios from 'axios'
import store from '@/store/index.js'
const http=axios.create({
    baseURL:"http://120.76.247.5:2001/api",
    timeout:100000
});
//请求拦截器 在发起请求之前的回调函数，可以在发起请求前做一些工作 比如自动带上token
http.interceptors.request.use((config)=>{
    // if(如果存在token  且config里面没有携带token是){
    //     config.headers.authorization=store.xxx
    // } post Delete patch put
    // console.log("拦截器调试===>",store.state.userStore.Authorization,config.method, ["post","delete","patch","put"].includes(config.method))
    if(store.state.userStore.Authorization&&["post","delete","patch","put"].includes(config.method)){
        config.headers.Authorization=store.state.userStore.Authorization;//自动带上token
    }
    // console.log('传值===>',config.params);
    return config;
},er=>{
    console.log("请求参数错误!",er);
});

//响应拦截器 用来处理服务器响应之后的数据 比如 data数据嵌套过深
http.interceptors.response.use((response)=>{
    if(response.data){
        if(response.data.data){
            return response.data.data;
        }
        return response.data;
    }
    return response;
},er=>{
    ElMessage({
        type:"error",
        message:"网络错误!"
    });
    console.log("网络请求错误!",er);
});

export default http;